package com.eascs.workflow.wf.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.controller.BaseMasterViewController;
import com.eascs.workflow.common.entity.wf.ProcessNodeTemplate;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.timeEffective.utils.CalculateTimeUtils;
import com.eascs.workflow.wf.form.ViewTemplateForm;
import com.eascs.workflow.wf.service.ProcessNodeTemplateService;
import com.eascs.workflow.wf.service.TemplateService;

@Controller
public class TemplateViewController extends BaseMasterViewController {

	@Inject
	private TemplateService templateService;

	@Inject
	private ProcessNodeTemplateService ProcessNodeTemplateService;
	
	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "systemSettings");
	}

	@RequestMapping(value = "/wf/template/view", method = RequestMethod.GET)
	public String viewTemplate(ViewTemplateForm fiewTemplateForm) {
		ProcessTemplate processTemplate = templateService.getProcessTemplate(fiewTemplateForm.getProcessNo());
		ViewContext.put("processTemplate", processTemplate);
		List<Object[]> data = ProcessNodeTemplateService.findNodeTemplatesByProcessId(fiewTemplateForm.getProcessNo());
		
		String updateUrl = "/nodeTemplate/add/index?processTemplateId=" + fiewTemplateForm.getProcessNo()
				+ "&nodeTemplateId=";
		String delUrl = "/nodeTemplate/del?processNo=" + fiewTemplateForm.getProcessNo()
				+ "&nodeTemplateId=";
		Button[] moreButtons = new Button[] { new RedirectButton("修改", updateUrl), new AjaxButton("删除", delUrl) };
		String[] titles = { "结点名称", "结点描述", "流程类型", "引用url", "节点时效", "角色名称", "顺序号", "意见模板" };
		SelectDataOption[] selectDataOptions = { null, null, new SelectDataOption("processNodeType"), null, null, null,
				null };
		PaginationInfo paginationInfo = new PaginationInfo(data, titles);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		if (processTemplate.getPublishStatus() != null
				&& processTemplate.getPublishStatus().equals(ProcessTemplate.PUBLISH_STATUS_NO)) {
			paginationInfo.setMoreButtons(moreButtons);
		}
		ViewContext.put("paginationInfo", paginationInfo);
		return "/wf/template/viewTemplate";
	}
}
